package com.landinfo.service.impl;

import com.landinfo.domain.GpsLocation;
import com.landinfo.repository.GpsLocationRepository;
import com.landinfo.service.GpsLocationService;
import com.landinfo.service.dto.GpsLocationDTO;
import com.landinfo.service.mapper.GpsLocationMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * Service Implementation for managing DataType.
 */
@Service
@Transactional
public class GpsLocationServiceImpl implements GpsLocationService {

    private final Logger log = LoggerFactory.getLogger(GpsLocationServiceImpl.class);

    private final GpsLocationRepository dataTypeRepository;

    private final GpsLocationMapper dataTypeMapper;

    public GpsLocationServiceImpl(GpsLocationRepository dataTypeRepository, GpsLocationMapper dataTypeMapper) {
        this.dataTypeRepository = dataTypeRepository;
        this.dataTypeMapper = dataTypeMapper;
    }

    /**
     * Save a dataType.
     *
     * @param gpsLocationDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public GpsLocationDTO save(GpsLocationDTO gpsLocationDTO) {
        log.debug("Request to save DataType : {}", gpsLocationDTO);
        GpsLocation gpsLocation = dataTypeMapper.toEntity(gpsLocationDTO);
        if (gpsLocation.getId() == null || "".equals(gpsLocation.getId())) {
            gpsLocation.setId(UUID.randomUUID().toString());
        }
        gpsLocation = dataTypeRepository.save(gpsLocation);
        return dataTypeMapper.toDto(gpsLocation);
    }

    @Override
    public Integer saveAll(List<GpsLocationDTO> dataTypeDTOs) {
        log.debug("Request to save DataType : {}", dataTypeDTOs);
        List<GpsLocation> gpsLocations = dataTypeMapper.toEntity(dataTypeDTOs);
        return dataTypeRepository.saveAll(gpsLocations).size();
    }

    /**
     * Get all the dataTypes.
     *
     * @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public List<GpsLocationDTO> findAll() {
        log.debug("Request to get all DataTypes");
        return dataTypeRepository.findAll().stream()
                .map(dataTypeMapper::toDto)
                .collect(Collectors.toCollection(LinkedList::new));
    }

    @Override
    @Transactional(readOnly = true)
    public List<GpsLocationDTO> getNewAllDevice() {
        Example example = null;

        dataTypeRepository.findAll(example);

        return null;
    }

    /**
     * Get one dataType by id.
     *
     * @param id the id of the entity
     * @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public GpsLocationDTO findOne(String id) {
        log.debug("Request to get DataType : {}", id);
        GpsLocation dataType = dataTypeRepository.findById(id).get();
        return dataTypeMapper.toDto(dataType);
    }

    /**
     * Delete the  dataType by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(String id) {
        log.debug("Request to delete DataType : {}", id);
        dataTypeRepository.deleteById(id);
    }

    @Override
    public void deleteByParentId(String parentId) {
        dataTypeRepository.deleteByParentId(parentId);
    }
}
